//
//  LeftSlideView.m
//  NewMusic
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "LeftSlideView.h"

@interface LeftSlideView ()

@end

@implementation LeftSlideView

- (void)dealloc
{
    [_scrollerView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
                     andArray:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSubView:array];
    }
    return self;
}
- (void)creatSubView:(NSArray *)array;
{
    self.scrollerView = [[UIScrollView alloc] initWithFrame:self.frame];
    self.scrollerView.contentSize = CGSizeMake(0, 20 + 70 * array.count);
    self.scrollerView.pagingEnabled = NO;
    self.scrollerView.showsVerticalScrollIndicator = NO;
    [self addSubview:self.scrollerView];
    [_scrollerView release];
    
    for (int i = 0; i < array.count * 5 + 4; i++)
    {
        UIView *view = [UIView new];
        view.frame = CGRectMake(0, 20 + 13 * i, 7, 1);
        view.backgroundColor = [UIColor colorWithRed:100 / 255.0 green:100 / 255.0 blue:100 / 255.0 alpha:1.0];
        [self.scrollerView addSubview:view];
        [view release];
        
        if (0 == (i + 1) % 5)
        {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(35, 5 + 13 * i, 45, 30);
            [button setTitle:[array objectAtIndex:(i + 1) / 5 - 1] forState:UIControlStateNormal];
            button.tag = 1330 + (i + 1) / 5 - 1;
            NSLog(@"%d", i);
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.scrollerView addSubview:button];
        }
    }
    
    self.ViewLeftLine = [[UIView alloc] initWithFrame:CGRectMake(0, 72, 20, 1)];
    self.ViewLeftLine.backgroundColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1.0];
    [self.scrollerView addSubview:self.ViewLeftLine];
    [_ViewLeftLine release];
    
}

- (void)buttonAction:(UIButton *)sender
{
    [UIView animateWithDuration:0.5 animations:^{
        self.ViewLeftLine.frame = CGRectMake(0, sender.frame.origin.y + 14, 20, 1);
    }];
    if (sender.tag < 1333 && sender.tag >= 1330)
    {
        [self.scrollerView setContentOffset:CGPointMake(0, 0) animated:YES];
    } else if (sender.tag > 1334 && sender.tag <= 1337) {
        [self.scrollerView setContentOffset:CGPointMake(0, 63) animated:YES];
    }
    [self.delegate leftSlideViewButton:sender];
}

- (void)setNumber:(NSInteger)number
{
    _number = 1330 + (number + 1) / 5 - 1;
    NSLog(@"%ld", _number);
    UIButton *button = (UIButton *)[self viewWithTag:_number];
    [UIView animateWithDuration:0.5 animations:^{
        self.ViewLeftLine.frame = CGRectMake(0, button.frame.origin.y + 14, 20, 1);
        NSLog(@"%f", button.frame.origin.y);
    }];
    if (button.tag < 1333 && button.tag >= 1330)
    {
        [self.scrollerView setContentOffset:CGPointMake(0, 0) animated:YES];
    } else if (button.tag > 1334 && button.tag <= 1337) {
        [self.scrollerView setContentOffset:CGPointMake(0, 63) animated:YES];
    }
}


@end
